import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

import '../extension/index.dart';
import 'index.dart';

class CommonDialogConfig {
  final Color confirmBorderColor;
  final Color confirmBackgroundColor;
  final Color cancelBorderColor;

  const CommonDialogConfig({
    this.confirmBorderColor = Colors.transparent,
    this.confirmBackgroundColor = Colors.transparent,
    this.cancelBorderColor = Colors.transparent,
  });
}

class CommonDialog {
  static final CommonDialog _instance = CommonDialog._internal();
  factory CommonDialog() => _instance;
  CommonDialog._internal();

  late final CommonDialogConfig _config;

  void init(CommonDialogConfig config) {
    _config = config;
  }

  static void oneButton({
    String? title,
    String description = '',
    String? confirmTitle,
    VoidCallback? onConfirm,
    String? tag,
    VoidCallback? onDismiss,
  }) {
    SmartDialog.show(
      builder: (context) {
        String titleStr = title ?? '温馨提示';
        return Container(
          width: ScreenUtil().screenWidth - 30.w,
          decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
          padding: EdgeInsets.fromLTRB(20.w, 15.w, 20.w, 25.w),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              if (titleStr.notEmpty) ...[
                TextWidget.moreLine(text: titleStr, fontSize: 18, color: Colors.black),
                SizedBox(height: 20.w),
              ],
              if (description.notEmpty) ...[
                TextWidget.moreLine(text: description, fontSize: 15, color: Colors.black),
                SizedBox(height: 25.w),
              ],
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 7.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                      child: ButtonWidget.text(
                        confirmTitle ?? '确定',
                        textColor: Colors.white,
                        borderColor: _instance._config.confirmBorderColor,
                        backgroundColor: _instance._config.confirmBackgroundColor,
                        borderWidth: 0.5,
                        borderRadius: 45.w,
                        height: 45.w,
                        alignment: Alignment.center,
                        onTap: () async {
                          await SmartDialog.dismiss();
                          onConfirm?.call();
                        },
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        );
      },
      backDismiss: false,
      clickMaskDismiss: false,
      tag: tag,
      onDismiss: onDismiss,
    );
  }

  static void twoButton({
    String? title,
    String description = '',
    String? cancelTitle,
    String? confirmTitle,
    VoidCallback? onCancel,
    VoidCallback? onConfirm,
    Widget? descriptionWidget,
    String? tag,
    VoidCallback? onDismiss,
  }) {
    SmartDialog.show(
      builder: (context) {
        List<Widget> children = [];

        String titleStr = title ?? '温馨提示';

        /// 标题
        if (titleStr.notEmpty) {
          children.addAll([
            TextWidget.moreLine(text: titleStr, fontSize: 16, color: '3D3D3D'.toColor, fontWeight: FontWeight.bold),
            SizedBox(height: 30.w),
          ]);
        }

        /// 描述
        if (descriptionWidget != null) {
          children.addAll([
            descriptionWidget,
            SizedBox(height: 25.w),
          ]);
        } else {
          if (description.notEmpty) {
            children.addAll([
              TextWidget.moreLine(text: description, fontSize: 14, color: Colors.black),
              SizedBox(height: 25.w),
            ]);
          }
        }

        return Container(
          width: ScreenUtil().screenWidth - 24.w * 2,
          decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(12.w)),
          padding: EdgeInsets.fromLTRB(20.w, 30.w, 20.w, 20.w),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              if (children.isNotEmpty) ...children,
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  ButtonWidget.text(
                    '取消',
                    textColor: const Color(0XFF4E5969),
                    borderColor: _instance._config.cancelBorderColor,
                    borderWidth: 1,
                    borderRadius: 42.w,
                    width: 134.w,
                    height: 42.w,
                    fontSize: 16,
                    alignment: Alignment.center,
                    onTap: () async {
                      await SmartDialog.dismiss();
                      onCancel?.call();
                    },
                  ),
                  ButtonWidget.text(
                    '确定',
                    textColor: Colors.white,
                    borderColor: _instance._config.confirmBorderColor,
                    backgroundColor: _instance._config.confirmBackgroundColor,
                    borderWidth: 1,
                    borderRadius: 42.w,
                    width: 134.w,
                    height: 42.w,
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                    alignment: Alignment.center,
                    onTap: () async {
                      await SmartDialog.dismiss();
                      onConfirm?.call();
                    },
                  ),
                ],
              ),
            ],
          ),
        );
      },
      backDismiss: false,
      clickMaskDismiss: false,
      tag: tag,
      onDismiss: onDismiss,
    );
  }

  ///关闭弹窗
  static Future closeDialog({String? tag}) async {
    await SmartDialog.dismiss(tag: tag);
  }

  static void showAttach({
    required BuildContext targetContext,
    required WidgetBuilder builder,
    Offset Function(Size selfSize)? scalePointBuilder,
    Offset Function(Offset targetOffset, Size targetSize)? targetBuilder,
    Alignment? alignment,
    String? tag,
    SmartAnimationType? animationType,
  }) {
    SmartDialog.showAttach(
      tag: tag,
      targetContext: targetContext,
      targetBuilder: targetBuilder,
      alignment: alignment,
      animationType: animationType,
      scalePointBuilder: scalePointBuilder,
      builder: builder,
      maskColor: Colors.transparent,
      animationTime: const Duration(milliseconds: 150),
    );
  }

  static Future hideAttach({String? tag}) async => await SmartDialog.dismiss(
        status: SmartStatus.attach,
        tag: tag,
      );
}
